<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/16
 * Time: 12:00
 */

namespace app\admin\controller;


use app\admin\model\UserAddress;
use app\common\controller\ReturnCode;
use think\Controller;
use think\Request;
use app\admin\model\User as U;
class User extends Controller
{
    private $aid;
    private $token;

    private $hasToken;

    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this -> aid = $request -> param('aid');
        $this -> token = $request -> param('token');
        $this -> uname = $request -> param('uname');
        $this -> uid = $request -> param('uid');
        $this -> page = $request -> param('page',1);
        $this -> hasToken = hasToken($this -> aid , $this -> token);
    }
    //列表
    public function index(){

        if(!isset($this -> aid) || empty($this -> token)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
//        $userModel = new U();
        if($this -> hasToken){
            $page = $this -> page;
            $res = U::where('uid','>',0)->paginate(10,false,['page'=> $page]);
//            die;
//            $res = db('user')->select();
            if($res){
                return json(['status' => 0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
            }
            return json(['status' => 1,'msg'=>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 10,'msg'=>ReturnCode::$return_code[10]]);

    }

    //添加
    public function useradd(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> uname)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $data = ['uname' =>$this -> uname];
            $res = U::create($data);
            if($res){
                return json(['status' => 0,'msg'=>ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg'=>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg'=>ReturnCode::$return_code[3]]);
    }

    //详情
    public function userinfo(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> uid)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $where = ['uid' =>$this -> uid];
//            $res = U::where($where)->field('uname,uimg,sex')->find();
            $res = db('user')->field('uname,uimg,sex')->where($where)->find();
            if($res){
                return json(['status' => 0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
            }
            return json(['status' => 1,'msg'=>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg'=>ReturnCode::$return_code[3]]);
    }
    //修改
    public function userup(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> uid) || empty($this -> uname)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $where = ['uid' =>$this -> uid];
            $res = U::update(['uname' => $this -> uname],$where);
            if($res){
                return json(['status' => 0,'msg'=>ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg'=>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg'=>ReturnCode::$return_code[3]]);
    }
    //软删除
    public function userdel(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> uid)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $where = ['uid' =>$this -> uid];
            $has = U::where($where) -> find();
            if(!$has){
                return json(['status' => 23,'msg'=>ReturnCode::$return_code[23]]);
            }
            $res = U::destroy($this -> uid);
            if($res){
                return json(['status' => 0,'msg'=>ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg'=>ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg'=>ReturnCode::$return_code[3]]);
    }

    /*----------------------------------------------------用户地址--------------------------------------------------------------------------*/
    //用户地址列表
    public function addresslist(){

        if(!isset($this -> aid) ||empty($this -> token) || empty($this -> uid)){
            return json(['status'=> 2,'msg'=> ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){

            $uInfo = U::where('uid','=',$this -> uid)->find();
            $res = UserAddress::where('openid','=',$uInfo['openid'])->paginate(10,false,['page'=>$this -> page]);
            if($res){
                return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0],'data' => $res]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3, 'msg' => ReturnCode::$return_code[3]]);
    }
    //添加
    public function address(Request $request){
        $sex = $request -> param('sex',1);
        $name = $request -> param('name');
        $phone = $request -> param('phone');
        $address = $request -> param('address');
        $lon = $request -> param('lon',0);
        $lat = $request -> param('lat',0);
//        dump(!check_mobile($phone));
//        die;
        if(!isset($this -> aid) ||empty($this -> token) || empty($this -> uid) || empty($name)|| !check_mobile($phone) || empty($address)){
            return json(['status'=> 2,'msg'=> ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $uInfo = U::where('uid','=',$this -> uid)->find();
            $data = [
                'openid' => $uInfo['openid'] ,
                'u_sex' => $sex,
                'u_name' => $name,
                'u_phone' => $phone,
                'address_name'=> $address,
                'lon' => $lon,
                'lat' => $lat,
                ];

            $has = UserAddress::where($data)->find();
            if($has){
                return json(['code' => 23,'msg' => ReturnCode::$return_code[23]]);
            }
            $res = UserAddress::create($data) -> allowField(true);
            if($res){
                return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3, 'msg' => ReturnCode::$return_code[3]]);
    }
    //详情
    public function addressinfo(Request $request){
        $addressId = $request -> param('addressid');
        if(!isset($this -> aid) ||empty($this -> token) || empty($this -> uid) || empty($addressId)){
            return json(['status'=> 2,'msg'=> ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $where = ['address_id' => $addressId];
            $res = UserAddress::where($where) -> find();
            if($res){
                return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0],'data' => $res]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3, 'msg' => ReturnCode::$return_code[3]]);
    }
    //修改
    public function addressup(Request $request){
        $addressId = $request -> param('addressid');
        $sex = $request -> param('sex',1);
        $name = $request -> param('name');
        $phone = $request -> param('phone');
        $address = $request -> param('address');
        $lon = $request -> param('lon',0);
        $lat = $request -> param('lat',0);
        if(!isset($this -> aid) ||empty($this -> token) || empty($this -> uid) || empty($phone) || empty($address) || empty($addressId) ){
            return json(['status'=> 2,'msg'=> ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $uInfo = U::where('uid','=',$this -> uid)->find();
            $where = ['address_id' => $addressId];
            $data = [
                'openid' => $uInfo['openid'] ,
                'u_sex' => $sex,
                'u_name' => $name,
                'u_phone' => $phone,
                'address_name'=> $address,
                'lon' => $lon,
                'lat' => $lat,
            ];
            $has = UserAddress::where($data)->find();
//            dump($has);
//            dump($data);
//            die;
            if($has){
                return json(['code' => 23,'msg' => ReturnCode::$return_code[23]]);
            }

            // 去除自身

            $res = UserAddress::update($data,$where)->allowField(true);
            if($res){
                return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3, 'msg' => ReturnCode::$return_code[3]]);
    }
    //删除
    public function addressdel(Request $request){
        $addressId = $request -> param('addressid');
        if(!isset($this -> aid) ||empty($this -> token) || empty($this -> uid) || empty($addressId)){
            return json(['status'=> 2,'msg'=> ReturnCode::$return_code[2]]);
        }
        if($this->hasToken){
            $where = ['address_id' => $addressId];
            $res = UserAddress::destroy($where);
            if($res){
                return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3, 'msg' => ReturnCode::$return_code[3]]);
    }

    /*----------------------------------------------------用户购物车--------------------------------------------------------------------------*/
    //用户购物车列表
    public function shoplist(){

    }
    //添加
    //详情
    //修改
    //删除

}